// ------- Admin Commands Script -------
// -------- Created by VRocker ---------

g_hashCmdLevels <- HashTable( "CommandLevels" );

function InitAdminCommandSystem()
{
	// Load hashes to determine the command level required
	LoadCmdLevelHashes();
}

function LoadCmdLevelHashes()
{
	g_hashCmdLevels.Load( "Hashes/CmdLevels.hsh" );
}

function SaveCmdLevelHashes()
{
	g_hashCmdLevels.Save( "Hashes/CmdLevels.hsh" );
}

function GetCmdLevel( name )
{
	local level = g_hashCmdLevels.Get( name );
	if ( level != null )
		return level;
	
	// All commands default to level 1 unless specified
	return 1;
}

function SetCmdLevel( name, level )
{
	g_hashCmdLevels.Add( name, level );
}

function ProcessAdminCommands( plr, level, cmd, args )
{
	local numArgs = 0;
	if ( args )
	{
		numArgs = NumTok( args, " " );
	}
	
	if ( cmd == "admins" )
	{
		// List current admins
		local i = 0, i1 = 0, admins = 0;
		local plr2 = 0;
		local level = 0;
		local cmdThing = 0;
		local adminOutput = "";
		while ( ( i < GetMaxPlayers() ) && ( i1 < GetPlayers() ) )
		{
			plr2 = FindPlayer( i );
			if ( plr2 )
			{
				level = GetPlayerLevel( plr2 );
				if ( level > 0 )
				{
					// Output as admin
					if ( cmdThing < 3 )
					{
						if ( adminOutput != "" ) adminOutput = adminOutput + ", " + plr2.Name + " (Level: " + level + ")";
						else adminOutput = plr2.Name + " (Level: " + level + ")";
						
						cmdThing++;
					}
					else
					{
						if ( admins < 3 ) Message( "----- Current Admins -----" );
						Message( adminOutput );
						
						adminOutput= plr2.Name + " (Level: " + level + ")";
						cmdThing = 0;
					}	
				}
				i1++;
			}
			i++;
		}
		
		if ( adminOutput != "" ) 
		{
			if ( admins < 3 ) Message( "----- Current Admins -----" );
			Message( adminOutput );
		}
		else Message( "No admins have been found on the server" );
		
		return true;
	}
	
	// Since any level can use the 'admins' command, we check the level here
	if ( level <= LVL_REGISTERED )
		return false;
	
	if ( cmd == "sun" )
	{
		if ( level >= GetCmdLevel( cmd ) )
		{
			Message( "-> Setting weather to: Sunny <-" );
			SetWeather( 0 );
		}
		else MessagePlayer( "You must be a level " + GetCmdLevel( cmd ) + " admin to use this command", plr, COL_CMDERROR );
		
		return true;
	}
	else if ( ( cmd == "storm" ) || ( cmd == "cloud" ) )
	{
		if ( level >= GetCmdLevel( cmd ) )
		{
			Message( "-> Setting weather to: Cloudy/Stormy <-" );
			SetWeather( 1 );
		}
		else MessagePlayer( "You must be a level " + GetCmdLevel( cmd ) + " admin to use this command", plr, COL_CMDERROR );
		
		return true;
	}
	else if ( cmd == "rain" )
	{
		if ( level >= GetCmdLevel( cmd ) )
		{
			Message( "-> Setting weather to: Rain <-" );
			SetWeather( 2 );
		}
		else MessagePlayer( "You must be a level " + GetCmdLevel( cmd ) + " admin to use this command", plr, COL_CMDERROR );
		
		return true;
	}
	else if ( cmd == "fog" )
	{
		if ( level >= GetCmdLevel( cmd ) )
		{
			Message( "-> Setting weather to: Foggy <-" );
			SetWeather( 3 );
		}
		else MessagePlayer( "You must be a level " + GetCmdLevel( cmd ) + " admin to use this command", plr, COL_CMDERROR );
		
		return true;
	}
		
	else if ( cmd == "kick" )
	{
		if ( level >= GetCmdLevel( cmd ) )
		{
			if ( args )
			{
				local plrText = GetTok( args, " ", 1 );
				local reason = "No Reason";
				if ( numArgs >= 2 )
					reason = GetTok( args, " ", 2, numArgs );

				local plr2 = null;
			
				if ( IsNum( plrText ) ) plr2 = FindPlayer( plrText.tointeger() );
				else plr2 = FindPlayer( plrText );
					
				if ( plr2 )
				{
					if ( plr2.ID != plr.ID )
					{
						if ( GetPlayerLevel( plr2 ) < level )
						{
							Message( "Admin " + plr + " has kicked " + plr2 + " [" + reason + "]" );
							KickPlayer( plr2 );
						}
						else MessagePlayer( "You cannot kick higher or equal level admins", plr, COL_CMDERROR );
					}
					else MessagePlayer( "You cannot kick yourself", plr, COL_CMDERROR );
				}
				else MessagePlayer( "Invalid player", plr, COL_CMDERROR );
			}
			else MessagePlayer( "Invalid arguments (/kick <player> [<reason>])", plr, COL_CMDERROR );
		}
		else MessagePlayer( "You must be a level " + GetCmdLevel( cmd ) + " admin to use this command", plr, COL_CMDERROR );
		
		return true;
	} // End of 'kick' function
	
	else if ( cmd == "ban" )
	{
		if ( level >= GetCmdLevel( cmd ) )
		{
			if ( args )
			{
				local plrText = GetTok( args, " ", 1 );
				local reason = "No Reason";
				if ( numArgs >= 2 )
					reason = GetTok( args, " ", 2, numArgs );

				local plr2 = null;
			
				if ( IsNum( plrText ) ) plr2 = FindPlayer( plrText.tointeger() );
				else plr2 = FindPlayer( plrText );
					
				if ( plr2 )
				{
					if ( plr2.ID != plr.ID )
					{
						if ( GetPlayerLevel( plr2 ) < level )
						{
							Message( "Admin " + plr + " has banned " + plr2 + " [" + reason + "]" );
							BanPlayer( plr2 );
						}
						else MessagePlayer( "You cannot ban higher or equal level admins", plr, COL_CMDERROR );
					}
					else MessagePlayer( "You cannot ban yourself", plr, COL_CMDERROR );
				}
				else MessagePlayer( "Invalid player", plr, COL_CMDERROR );
			}
			else MessagePlayer( "Invalid arguments (/ban <player> [<reason>])", plr, COL_CMDERROR );
		}
		else MessagePlayer( "You must be a level " + GetCmdLevel( cmd ) + " admin to use this command", plr, COL_CMDERROR );
		
		return true;
	} // End of 'ban' function
	
	else if ( cmd == "setlevel" )
	{
		if ( level >= GetCmdLevel( cmd ) )
		{
			if ( ( args ) && ( numArgs == 2 ) )
			{
				local plrText = GetTok( args, " ", 1 );
				local setLvl = GetTok( args, " ", 2 );

				local plr2 = null;
			
				if ( IsNum( plrText ) ) plr2 = FindPlayer( plrText.tointeger() );
				else plr2 = FindPlayer( plrText );
					
				if ( plr2 )
				{
					if ( plr2.ID != plr.ID )
					{
						if ( setLvl < LVL_REGISTERED )
						{
							if ( setLvl < level )
							{
								local oldLevel = GetPlayerLevel( plr2 );
								if ( oldLevel < level )
								{
									if ( oldLevel < setLvl )
										Message( "Admin " + plr + " has promoted player " + plr2 + " to level " + setLvl );
									else
										Message( "Admin " + plr + " has demoted player " + plr2 + " to level " + setLvl );
								
									SetPlayerLevel( plr2, setLvl );
								}
								else MessagePlayer( "You cannot set the level of higher or equal level admins", plr, COL_CMDERROR );
							}
							else MessagePlayer( "You cannot set a level higher or equal to your own", plr, COL_CMDERROR );
						}
						else MessagePlayer( "Invalid level", plr, COL_CMDERROR );
					}
					else MessagePlayer( "You cannot set your own level", plr, COL_CMDERROR );
				}
				else MessagePlayer( "Invalid player", plr, COL_CMDERROR );
			}
			else MessagePlayer( "Invalid arguments (/setlevel <player> <level>)", plr, COL_CMDERROR );
		}
		else MessagePlayer( "You must be a level " + GetCmdLevel( cmd ) + " admin to use this command", plr, COL_CMDERROR );
		
		return true;
	} // End of 'setlevel' function
	
	else if ( cmd == "setcmdlevel" )
	{
		if ( level >= GetCmdLevel( cmd ) )
		{
			if ( ( args ) && ( numArgs == 2 ) )
			{
				local setCmd = GetTok( args, " ", 1 );
				local setLvl = GetTok( args, " ", 2 );
			
				if ( setLvl.tointeger() > LVL_REGISTERED )
				{
					Message( "Admin " + plr + " has changed the required level to use " + setCmd + " to level " + setLvl );
					
					SetCmdLevel( setCmd, setLvl );
					
					SaveCmdLevelHashes();
				}
				else MessagePlayer( "Invalid level", plr, COL_CMDERROR );
			}
			else MessagePlayer( "Invalid arguments (/setlevel <cmd> <level>)", plr, COL_CMDERROR );
		}
		else MessagePlayer( "You must be a level " + GetCmdLevel( cmd ) + " admin to use this command", plr, COL_CMDERROR );
		
		return true;
	} // End of 'setlevel' function
}